﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public enum ESound
{
	MonsterGhostAway,
	Skill_Heal,
	Tile_Buf,
	Tile_Debuff,
	RewardGacha_popup,
	RewardGacha_select,
	Skill_Confuse_Bind_Cast,
	Skill_Confuse_Bind_Debuff,
	Skill_Done,
	Tile_Bumpy,
	GameStart,//voice
	MyTurn_Dice,//voice
	DiceNum_1,//voice
	DiceNum_2,
	DiceNum_3,
	DiceNum_4,
	DiceNum_5,
	DiceNum_6,
	DiceNum_7,
	DiceNum_8,
	DiceNum_9,
	DiceNum_10,
	DiceNum_11,
	DiceNum_12,
	Voice_BattleWin_01,
	Voice_BattleWin_02,
	Voice_BattleWin_03,
	Voice_BattleLose_01,
	Voice_BattleLose_02,
	Voice_GoalWin_01,
	Voice_GoalWin_02,
	Voice_GoalWin_03,
	Voice_GoalLose_01,
	Voice_GoalLose_02,
	Voice_Skill_01,
	Voice_Skill_02,
	Voice_Skill_03,
	Voice_Skill_04,

}

public class EmotionSound
{
	public int id;
	public AudioClip clip;
}

public class SoundManager : MonoBehaviour {
	
	private static SoundManager instance;
	
	public AudioClip[] soundList;

	Dictionary<ESound, AudioClip> soundDic = new Dictionary<ESound, AudioClip>();
	Dictionary<int , List<EmotionSound> > emotionSound = new Dictionary<int, List<EmotionSound> >();
	//public Dictionary<string, int> soundDic = new Dictionary<string, int>();
	
	public static SoundManager Instance{
		get{
			return instance;
		}
	}
	
	void Awake()
	{
		instance = this;
		soundDic.Add ( ESound.MonsterGhostAway , Resources.Load("Sound/monster_ghost_away") as AudioClip );
		soundDic.Add ( ESound.Skill_Heal , Resources.Load("Sound/skill_heal") as AudioClip );
		soundDic.Add ( ESound.Tile_Buf , Resources.Load("Sound/tile_buff") as AudioClip );
		soundDic.Add ( ESound.Tile_Debuff , Resources.Load("Sound/tile_debuff") as AudioClip );
		soundDic.Add ( ESound.GameStart , Resources.Load("Sound/Voice/system_voice_start") as AudioClip );
		soundDic.Add ( ESound.MyTurn_Dice , Resources.Load("Sound/Voice/system_voice_dice") as AudioClip );
		soundDic.Add ( ESound.DiceNum_1 , Resources.Load("Sound/Voice/system_voice_num_01") as AudioClip );
		soundDic.Add ( ESound.DiceNum_2 , Resources.Load("Sound/Voice/system_voice_num_02") as AudioClip );
		soundDic.Add ( ESound.DiceNum_3 , Resources.Load("Sound/Voice/system_voice_num_03") as AudioClip );
		soundDic.Add ( ESound.DiceNum_4 , Resources.Load("Sound/Voice/system_voice_num_04") as AudioClip );
		soundDic.Add ( ESound.DiceNum_5 , Resources.Load("Sound/Voice/system_voice_num_05") as AudioClip );
		soundDic.Add ( ESound.DiceNum_6 , Resources.Load("Sound/Voice/system_voice_num_06") as AudioClip );
		soundDic.Add ( ESound.DiceNum_7 , Resources.Load("Sound/Voice/system_voice_num_07") as AudioClip );
		soundDic.Add ( ESound.DiceNum_8 , Resources.Load("Sound/Voice/system_voice_num_08") as AudioClip );
		soundDic.Add ( ESound.DiceNum_9 , Resources.Load("Sound/Voice/system_voice_num_09") as AudioClip );
		soundDic.Add ( ESound.DiceNum_10 , Resources.Load("Sound/Voice/system_voice_num_10") as AudioClip );
		soundDic.Add ( ESound.DiceNum_11 , Resources.Load("Sound/Voice/system_voice_num_11") as AudioClip );
		soundDic.Add ( ESound.DiceNum_12 , Resources.Load("Sound/Voice/system_voice_num_12") as AudioClip );
		soundDic.Add ( ESound.Voice_BattleWin_01 , Resources.Load("Sound/Voice/system_voice_battle_win_01") as AudioClip );
		soundDic.Add ( ESound.Voice_BattleLose_01 , Resources.Load("Sound/Voice/system_voice_battle_lose_01") as AudioClip );
		soundDic.Add ( ESound.Voice_Skill_01 , Resources.Load("Sound/Voice/system_voice_skill_01") as AudioClip );
	}
	
	public void PlaySound(int index)
	{
		Play_Effect_Sound(soundList[index]);
	}

	public void PlaySoundDiceNum(int num)
	{
		if(num>=1 && num<=12)
		{
			int esoundNum = (int)ESound.DiceNum_1 + num-1;
			PlaySound( (ESound)esoundNum);
		}
	}

	public void PlaySoundVoiceBattleWin()
	{
		PlaySound(ESound.Voice_BattleWin_01);
	}

	public void PlaySoundVoiceBattleLose()
	{
		PlaySound(ESound.Voice_BattleLose_01);
	}

	public void PlayEmoticonVoice(int charID, int emotionID)
	{
		if( !emotionSound.ContainsKey(charID) )
		{
			emotionSound.Add (charID, new List<EmotionSound>() );
			PlayEmoticonVoiceNew(charID, emotionID);
		}
		else
		{
			foreach( EmotionSound emosound in emotionSound[charID] )
			{
				if(emosound.id == emotionID)
				{
					Play_Effect_Sound(emosound.clip);
					return;
				}
			}
			PlayEmoticonVoiceNew(charID, emotionID);
		}
	}

	void PlayEmoticonVoiceNew(int charID, int emotionID)
	{
		Debug.LogError("PlayEmoticonVoiceNew  char="+charID+"  emoID="+emotionID);
		EmotionSound emoSound = new EmotionSound();
		emoSound.id = emotionID;
		string pathRes = string.Format("Sound/emoticon/emoticon_{0:00}_{1:00}",charID,emotionID);
		emoSound.clip = Resources.Load(pathRes) as AudioClip;
		if(emoSound.clip!=null)
		{
			emotionSound[charID].Add ( emoSound);
			Play_Effect_Sound(emoSound.clip);
		}
	}
	
	public void PlaySound(ESound esound)
	{
		if(!soundDic.ContainsKey(esound))
		{
			if(esound== ESound.RewardGacha_popup)
				soundDic.Add ( ESound.RewardGacha_popup , Resources.Load("Sound/gacha_02") as AudioClip );
			else if(esound== ESound.RewardGacha_select)
				soundDic.Add ( ESound.RewardGacha_select , Resources.Load("Sound/gacha_01") as AudioClip );
			else if(esound== ESound.Skill_Confuse_Bind_Cast)
				soundDic.Add ( ESound.Skill_Confuse_Bind_Cast , Resources.Load("Sound/Skill_Confuse_Bind_Cast") as AudioClip );
			else if(esound== ESound.Skill_Confuse_Bind_Debuff)
				soundDic.Add ( ESound.Skill_Confuse_Bind_Debuff , Resources.Load("Sound/Skill_Confuse_Bind_Debuff") as AudioClip );
			else if(esound== ESound.Skill_Done)
				soundDic.Add ( ESound.Skill_Done , Resources.Load("Sound/skill_done") as AudioClip );
			else if(esound== ESound.Tile_Bumpy)
				soundDic.Add ( ESound.Tile_Bumpy , Resources.Load("Sound/tile_bumpy") as AudioClip );
			else if(esound== ESound.Voice_GoalWin_01)
				soundDic.Add ( ESound.Voice_GoalWin_01 , Resources.Load("Sound/Voice/system_voice_goal_win_01") as AudioClip );
			else if(esound== ESound.Voice_GoalLose_02)
				soundDic.Add ( ESound.Voice_GoalLose_02 , Resources.Load("Sound/Voice/system_voice_goal_lose_02") as AudioClip );
		}

		if(soundDic.ContainsKey(esound))
			Play_Effect_Sound( soundDic[esound]);
	}
	
	public void Play_Effect_Sound(AudioClip clip)
	{
		audio.loop=false;
		if(clip != null) {
			//glog.debug ("play sound");
			audio.PlayOneShot(clip);
		}
	}

	public void PlaySoundLoop(int index)
	{
		if( soundList[index]!=null)
		{
			audio.loop=true;
			audio.clip = soundList[index];
			audio.Play ();
		}

	}

	public void AudioStop()
	{
		audio.Stop();
	}

}